mini.parse();

function ArticlesSortFormHandler() {
};

ArticlesSortFormHandler.instance = new ArticlesSortFormHandler();

//获得全局的文章分类表单对象
ArticlesSortFormHandler.articlesSortForm = new mini.Form("articlesSortForm");
ArticlesSortFormHandler.formData = {};

// 文章分类状态数据
ArticlesSortFormHandler.bagStateData = [
	{text: "有效",value: 1},
	{text: "无效",value: 0}
];
mini.get("bagState").setData(ArticlesSortFormHandler.bagStateData);

/**
 * 保存（添加或编辑）文章分类数据的方法
 */
ArticlesSortFormHandler.save = function(){
	var articlesSortData = ArticlesSortFormHandler.articlesSortForm.getData();
	articlesSortData.bagId = ArticlesSortFormHandler.formData.bagId;
	
    ArticlesSortFormHandler.articlesSortForm.validate();
    
    if (ArticlesSortFormHandler.articlesSortForm.isValid() == false) return;
    if(mini.get("topBagType").getChecked()) articlesSortData.bagParent = "0";
    
    var json = mini.encode(articlesSortData);
    $.ajax({
        url: __root + "/saveOrUpdateBasInfo.do",
        type: "POST",
        data: {
        	basInfo: json,
        	lslUser: gbOperateUser,
    		lslAddress: gbOperateIp
        },
        cache: false,
        dataType: 'text',
        success: function () {
        	MiniuiTools.miniInfo("系统提示","保存成功！", function(action){
        		ArticlesSortFormHandler.closeCurrentWin("save");
        	});
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	MiniuiTools.miniError("系统提示", jqXHR.responseText, function(){
            	ArticlesSortFormHandler.closeCurrentWin();
            });
        }
    });
}

/**
 * 读取并选中默认父亲节点
 * @param {} bagParent
 */
ArticlesSortFormHandler.getParentBasInfo = function(bagParent){
    $.ajax({ 
        url: __root + "/getBasInfoById.do?bagId=" + bagParent,
        cache: false,
        dataType: 'json',
        success: function (parentText) {
        	//动态加载节点时，需同时设置setValue，setText，且必须先setValue，然后再setText
        	if(parentText == null || parentText == "" || parentText.bagId == null 
        		|| parentText.bagId == "undefined"
        		|| parentText.bagId == "0"){
        		mini.get("bagParentTree").setEnabled(false);
				mini.get("topBagType").setChecked(true);
        	}else{
        		mini.get("bagParentTree").setValue(parentText.bagId);
	            mini.get("bagParentTree").setText(parentText.bagName);
        	}
        }
    });
}

/**
 * 标准方法接口定义
 * @param {} data
 */
ArticlesSortFormHandler.passData = function(data) {
    if (data.action == "edit") {
        //跨页面传递的数据对象，克隆后才可以安全使用
        data = mini.clone(data);
        ArticlesSortFormHandler.formData.bagId = data.bagId;
        //根据id获取对应的文章分类信息
        $.ajax({ 
            url: __root + "/getBasInfoById.do?bagId=" + data.bagId,
            cache: false,
            dataType: 'json',
            success: function (text) {
                ArticlesSortFormHandler.articlesSortForm.setData(text);
				ArticlesSortFormHandler.getParentBasInfo(text.bagParent);                
            }
        });
    }else{
    	if(data.bagId != null && data.bagId != "0"){
    		//新增文章分类时，选定默认父节点
			mini.get("bagParentTree").setValue(data.bagId);
            mini.get("bagParentTree").setText(data.bagName);
    	}else{
    		//如果没有选中数据，就默认父亲节点为顶级节点
    		mini.get("bagParentTree").setEnabled(false);
    		mini.get("topBagType").setChecked(true);
    	}
    	
    	//默认分类状态为有效
    	mini.get("bagState").setValue(1);
    }
}

/**
 * 顶级分类mini-checkbox选择改变事件
 * @param {} e
 */
ArticlesSortFormHandler.selectedTopBagType = function(e){
 	var isChecked = this.getChecked();
 	if(isChecked){
 		mini.get("bagParentTree").setEnabled(false);
 	}else{
 		mini.get("bagParentTree").setEnabled(true);
 	}
}

/**
 * 关闭窗口
 * @param {} action
 */
ArticlesSortFormHandler.closeCurrentWin = function(action) {
    if (window.CloseOwnerWindow) window.CloseOwnerWindow(action);
    else window.close();
}

ArticlesSortFormHandler.closeMiniWin = function(){
	ArticlesSortFormHandler.closeCurrentWin("cancel");
}